package view.components
{
	import assets.view.components.MenuButtonViewSymbol;
	
	import flash.display.Graphics;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextFieldAutoSize;
	
	import model.vo.ViewVO;

	public class MenuButtonView extends MenuButtonViewSymbol
	{
		public static const NAME:String 				= "ButtonView";
		public static const CLICKED:String				= NAME + "Clicked";
		
		private var buttonData:Object;
		
		private var buttonText:String;
		
		private var bckgrdColor:Number;
		private var bckgrdSelectColor:Number;
		private var bckgrdOverColor:Number;
		
		private var labelColor:Number;
		private var labelSelectColor:Number;
		private var labelOverColor:Number;
		
		private var buttonWidth:Number;
		private var buttonHeight:Number;
		
		private var btnIndex:int;
		
		private var viewData:ViewVO;
		
		
		public function MenuButtonView()
		{
			this.alpha = 0;
			this.buttonMode = true;
		}
		
		public function init( data:Object, viewVO:ViewVO, index:int ):void
		{
			buttonData 	= data;
			viewData 	= viewVO;
			btnIndex 	= index;
			
			setMenuButtonViewProperties();
			buildMenuButtonView();
			
			addEvents();
		}
		
		public function select():void
		{
			removeEvents();
			
			this.buttonMode = false;
			
			FX.addShadow( label, true );
			FX.tint( bckgrdContainer, bckgrdSelectColor );
			label.textColor = labelSelectColor;
		}
		
		public function unselect():void
		{
			this.buttonMode = true;
			
			FX.removeFilters( label );
			FX.tint( bckgrdContainer, bckgrdColor );
			label.textColor = labelColor;
			
			addEvents();
		}
		
		private function setMenuButtonViewProperties():void
		{
			buttonText 			= buttonData.label;
			
			labelColor 			= viewData.labelColor;
			labelSelectColor	= viewData.labelSelectedColor;
			labelOverColor 		= viewData.labelOverColor;
			
			bckgrdColor 		= viewData.btnBckgrdColor;
			bckgrdSelectColor 	= viewData.btnBckgrdSelectedColor;
			bckgrdOverColor 	= viewData.btnBckgrdOverColor;
			
			buttonHeight 		= viewData.MENU_HEIGHT;
		}
		
		private function buildMenuButtonView():void
		{
			label.autoSize 	= TextFieldAutoSize.LEFT;
			label.text 		= buttonText;
			
			buttonWidth 	= label.width + 10;
			
			paintBckgrd();
			paintCover();
			
			label.x = Math.round( (bckgrdContainer.width / 2) - (label.width / 2) );
			label.y = Math.round( (bckgrdContainer.height / 2) - (label.height / 2) );
		}
		
		private function paintBckgrd():void
		{
			var g:Graphics = bckgrdContainer.graphics;
			g.clear();
			g.beginFill( bckgrdColor, 1 );
			g.drawRect( 0, 0, buttonWidth, buttonHeight );
			g.endFill();
		}
		
		private function paintCover():void
		{
			var g:Graphics = coverContainer.graphics;
			g.clear();
			g.beginFill( 0xff0000, 0 );
			g.drawRect( 0, 0, buttonWidth, buttonHeight );
			g.endFill();
		}
		
		private function addEvents():void
		{
			this.addEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
			this.addEventListener( MouseEvent.MOUSE_OVER, mouseOverHandler );
			this.addEventListener( MouseEvent.MOUSE_OUT, mouseOutHandler );
		}
		
		private function removeEvents():void
		{
			this.removeEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
			this.removeEventListener( MouseEvent.MOUSE_OVER, mouseOverHandler );
			this.removeEventListener( MouseEvent.MOUSE_OUT, mouseOutHandler );
		}
		
		private function mouseDownHandler( e:MouseEvent ):void
		{
			select();
			dispatchEvent( new Event( CLICKED ) );
		}
		
		private function mouseOverHandler( e:MouseEvent ):void
		{
			FX.addShadow( label );
			FX.tint( bckgrdContainer, bckgrdOverColor );
			label.textColor = labelOverColor;
		}
		
		private function mouseOutHandler( e:MouseEvent ):void
		{
			FX.removeFilters( label );
			FX.tint( bckgrdContainer, bckgrdColor );
			label.textColor = labelColor;
		}
		
		public function get data():Object
		{
			return buttonData;
		}
		
		public function get index():int
		{
			return btnIndex;
		}
	}
}